package com.example.yaseademo

import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import kotlinx.android.synthetic.main.activity_player.*


class PlayerActivity : AppCompatActivity() {
    private val exoPlayer by lazy { SimpleExoPlayer.Builder(this).build() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player)

        pv.player = exoPlayer
        exoPlayer.playWhenReady = true
        //拉流url
        // val url = "rtmp://live-push.bilivideo.com/live-bvc/"
        val createMediaSource = ProgressiveMediaSource.Factory(DefaultHttpDataSourceFactory("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2"))
                .createMediaSource(Uri.parse("http://tb-video.bdstatic.com/tieba-smallvideo-transcode-crf/19536676_51608df5d672273c6e536378215d5ac5_0.mp4"))
        exoPlayer.prepare(createMediaSource)

        NetworkSpeedChecker.getNetSpeed(this, 1, getNetSpeedDone = {
            Log.d("netSpeed", "$it b/s")
        })

    }


    override fun onDestroy() {
        super.onDestroy()
        NetworkSpeedChecker.cancelJob()
        exoPlayer.apply {
            stop()
            release()
        }

    }
}